﻿using Ozone.Help;
using Ozone.Models;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ozone.DAL
{
    public class OperLogDAL
    {

        private static List<sys_operlog> _allsys_operlogs;
        /// <summary>
        /// 查询系统日志
        /// </summary>
        /// <param name="Starttime"></param>
        /// <param name="EndTime"></param>
        /// <param name="FID"></param>
        /// <returns></returns>
        public static List<sys_operlog> GetLogList(DateTime Starttime, DateTime EndTime, string FID)
        {
            try
            {
                SqlSugarScope db = SqlSugarOper.Instance.GetDbClient();
                if (!string.IsNullOrEmpty(FID))
                {
                    //根据ID查询
                    _allsys_operlogs = db.Queryable<sys_operlog>()
                    .Where(p => p.FID == Convert.ToInt32(FID))
                    .OrderBy(p => p.CreateDate, OrderByType.Desc)
                    .ToList();
                }
                else
                {
                    //根据时间查询
                    _allsys_operlogs = db.Queryable<sys_operlog>()
                    .Where(p => p.CreateDate >= Starttime && p.CreateDate <= EndTime)
                    .OrderBy(p => p.CreateDate, OrderByType.Desc)
                    .ToList();
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("查询系统日志发生异常", ex);
                _allsys_operlogs = null;
            }
            return _allsys_operlogs;
        }

        /// <summary>
        /// 添加日志信息
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        public static bool AddLog(sys_operlog Model)
        {
            bool IsSuccess = false;
            try
            {
                SqlSugarScope db = SqlSugarOper.Instance.GetDbClient();
                if (Model != null)
                {
                    IsSuccess = db.Insertable<sys_operlog>(Model).ExecuteCommand() > 0;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("添加日志异常", ex);
                IsSuccess = false;
            }
            LogHelper.Debug("添加日志异常");

            return IsSuccess;
        }




        /// <summary>
        /// 删除日志信息
        /// </summary>
        /// <param name="FID"></param>
        /// <returns></returns>
        public static bool DeleteLog(int FID)
        {
            bool IsSuccess = false;
            try
            {
                SqlSugarScope db = SqlSugarOper.Instance.GetDbClient();
                if (FID > 0)
                {
                    IsSuccess = db.Deleteable<sys_operlog>().Where(p => p.FID == FID).ExecuteCommand() > 0;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("删除日志异常", ex);
                IsSuccess = false;
            }
            return IsSuccess;
        }

        /// <summary>
        /// 修改信息
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        public static bool UpdateLog(sys_operlog Model)
        {
            bool IsSuccess = false;
            try
            {
                SqlSugarScope db = SqlSugarOper.Instance.GetDbClient();
                if (Model != null)
                {
                    IsSuccess = db.Updateable<sys_operlog>()
                    .SetColumns(p => p.OperationModel == Model.OperationModel)
                    .SetColumns(p => p.OperationContentJson == Model.OperationContentJson)
                    .SetColumns(p => p.OperaterCode == Model.OperaterCode)
                    .SetColumns(p => p.OperaterName == Model.OperaterName)
                    .SetColumns(p => p.OperateRemark == Model.OperateRemark)
                    .Where(p => p.FID == Model.FID)
                    .ExecuteCommand() > 0;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("修改日志异常", ex);
                IsSuccess = false;
            }
            return IsSuccess;
        }
    }
}
